package controller

import (
	"fmt"
	"gin-shop-admin/models"
	"gin-shop-admin/repository"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetGoodsCategory(c *gin.Context) {
	request_type := c.Query("type")
	pageNum := c.Query("pagenum")
	pageSize := c.Query("pagesize")
	total, cates, err := repository.GetGoodsCategory(request_type, pageNum, pageSize)
	if err != nil {
		c.JSON(201, gin.H{"data": nil, "total": total})
	} else {
		c.JSON(http.StatusOK, gin.H{"data": cates, "total": total})
	}
	return
}

func AddOneCategory(c *gin.Context)  {
	var category models.Category
	err := c.BindJSON(&category)
	if err != nil {
		c.JSON(201, gin.H{"msg": err})
		return
	}
	err = repository.AddOneCategory(category)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg": "添加分类成功"})
	}
	return
}

func UpdateOneCategory(c *gin.Context)  {
	var cate models.Category
	id, ok := c.Params.Get("id")
	err := c.BindJSON(&cate)
	if !ok || err != nil{
		c.JSON(201, gin.H{"msg": "参数获取失败", "error": err})
		return
	}
	cat_id, err := strconv.Atoi(id)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
		return
	}
	cate.ID = cat_id
	err = repository.UpdateOneCategory(cate)
	if err != nil {
		c.JSON(203, gin.H{"msg": err})
		return
	}
	c.JSON(http.StatusOK, gin.H{"msg": "修改成功"})
	return
}

func DeleteOneCategory(c *gin.Context)  {
	id, ok := c.Params.Get("id")
	cat_id, err := strconv.Atoi(id)
	if !ok || err != nil{
		c.JSON(201, gin.H{"msg": "参数获取失败", "error": err})
		return
	}
	err = repository.DeleteOneCategory(cat_id)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
		return
	}
	c.JSON(http.StatusOK, gin.H{"msg": "删除成功"})
	return
}

func GetCategoryAttribute(c *gin.Context)  {
	cat_id, cat_id_ok := c.Params.Get("cat_id")
	sel, sel_ok := c.GetQuery("sel")
	if !cat_id_ok || !sel_ok{
		c.JSON(201, gin.H{"msg": "参数获取失败", "data": nil})
		return
	}
	CatID,err := strconv.Atoi(cat_id)
	if err != nil {
		c.JSON(202, gin.H{"msg": "参数类型有误", "data": nil})
		return
	}
	attrs, err := repository.GetCategoryAttribute(CatID, sel)
	if err!=nil {
		c.JSON(203, gin.H{"msg": err, "data": nil})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg": "获取参数列表成功", "data": attrs})
	}
	return
}

func GetCategoryOneAttribute(c *gin.Context)  {
	id, ok1 := c.Params.Get("cat_id")
	id2, ok2 := c.Params.Get("attr_id")
	cat_id, err1 := strconv.Atoi(id)
	attr_id, err2 := strconv.Atoi(id2)
	sel, ok3 := c.GetQuery("attr_sel")
	fmt.Println(cat_id, attr_id, sel)
	if !ok1 || !ok2 || !ok3 || err1 != nil || err2 != nil{
		c.JSON(201, gin.H{"msg": "参数获取失败"})
		return
	}
	attr, err := repository.GetCategoryOneAttribute(cat_id, attr_id, sel)
	if err!=nil {
		c.JSON(202, gin.H{"msg": err, "data": nil})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg": "获取参数成功", "data": attr})
	}
	return
}

func AddCategoryAttribute(c *gin.Context)  {
	var attr models.ShopAttribute
	id, ok := c.Params.Get("cat_id")
	cat_id, err := strconv.Atoi(id)
	err2 := c.BindJSON(&attr)
	if !ok || err != nil || err2 != nil{
		c.JSON(201, gin.H{"msg": "参数获取失败", "error": err})
		return
	}
	attr.CatID = cat_id
	err = repository.AddCategoryAttribute(attr)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg":" 添加成功"})
	}
	return
}

func UpdateCategoryAttribute(c *gin.Context)  {
	var attr models.ShopAttribute
	id, ok1 := c.Params.Get("cat_id")
	id2, ok2 := c.Params.Get("attr_id")
	cat_id, err1 := strconv.Atoi(id)
	attr_id, err2 := strconv.Atoi(id2)
	err3 := c.BindJSON(&attr)
	fmt.Println(cat_id, attr_id, attr)
	if !ok1 || !ok2 || err1 != nil || err2 != nil || err3 != nil{
		c.JSON(201, gin.H{"msg": "参数获取失败"})
		return
	}
	attr1 := models.ShopAttribute{ID:attr_id, CatID:cat_id, AttrSel:attr.AttrSel}
	err := repository.UpdateCategoryAttribute(attr1, attr.Name, attr.AttrVals)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg": "修改参数成功"})
	}
	return
}

func DeleteOneAttribute(c *gin.Context)  {
	id, ok1 := c.Params.Get("cat_id")
	id2, ok2 := c.Params.Get("attr_id")
	cat_id, err1 := strconv.Atoi(id)
	attr_id, err2 := strconv.Atoi(id2)
	if !ok1 || !ok2 || err1 != nil || err2 != nil{
		c.JSON(201, gin.H{"msg": "参数获取失败"})
		return
	}
	err := repository.DeleteOneAttribute(cat_id, attr_id)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg": "修改参数成功"})
	}
	return
}